﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LeanEngine.Collections
{
    /// <summary>
    /// Extension methods for System.Collections.Generic.Dictionary.
    /// </summary>
    public static class DictionaryExtensions
    {
        /// <summary>
        /// Adds the specified key and value to the dictionary checking for duplicity.
        /// </summary>
        /// <exception cref="System.ArgumentNullException"></exception>
        /// <exception cref="System.ArgumentException"></exception>
        /// <typeparam name="TKey">The type of the keys in the dictionary.</typeparam>
        /// <typeparam name="TValue">The type of the values in the dictionary.</typeparam>
        /// <param name="dictionary">The dictionary instance.</param>
        /// <param name="key">The key of the element to add.</param>
        /// <param name="value">The value of the element to add. The value can be null of reference types.</param>
        /// <param name="checkDuplicated">The value indicating to check or not if the value already exists before add.</param>
        public static void Add<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key, TValue value, bool checkDuplicated)
        {
            if (dictionary != null)
            {
                if (checkDuplicated)
                {
                    if (dictionary.ContainsKey(key))
                    {
                        return;
                    }
                }

                dictionary.Add(key, value);
            }
        }
    }
}
